package com.example.usharingtest.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.example.usharingtest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by X on 2017/9/21.
 */
public class FragmentOper {

    /**
     * FragmentActivity 实例
     */
    private FragmentActivity context;

    /**
     * Fragment 管理器
     */
    private FragmentManager fManager;

    /**
     * 装Fragment的容器
     */
    private int containerId;




    private StudyFragment studyFragment;
    private FoodFragment foodFragment;
    private TravelFragment travelFragment;
    private MyInfoFragment myInfoFragment;
    private LoginFragment loginFragment;

    public FragmentOper(FragmentActivity context,int containerId){
        super();
        this.context = context;
        this.containerId = containerId;
        fManager = this.context.getSupportFragmentManager();
    }



    public void onCheckedChanged( int checkedId,int userid) {
        FragmentTransaction ft = fManager.beginTransaction();
        if (studyFragment != null) {
            ft.hide(studyFragment);
        }
        if (foodFragment != null) {
            ft.hide(foodFragment);
        }
        if (travelFragment != null) {
            ft.hide(travelFragment);
        }

        if(myInfoFragment != null){
            ft.hide(myInfoFragment);
        }

        if(loginFragment != null){
            ft.hide(loginFragment);
        }

        switch (checkedId) {
            case R.id.study_image:
                if (studyFragment == null) {
                    studyFragment = new StudyFragment();
                    ft.add(R.id.study_fragment, studyFragment);
                } else {
                    ft.show(studyFragment);
                }
                break;
            case R.id.food_image:
                if (foodFragment == null) {
                    foodFragment = new FoodFragment();
                    ft.add(R.id.study_fragment, foodFragment);
                } else {
                    ft.show(foodFragment);
                }
                break;
            case R.id.travel_image:
                if (travelFragment == null) {
                    travelFragment = new TravelFragment();
                    ft.add(R.id.study_fragment, travelFragment);
                } else {
                    ft.show(travelFragment);
                }
                break;
            case R.id.myinfo:
                if(userid == -1) {
                    if (loginFragment == null) {
                        loginFragment = new LoginFragment();
                        ft.add(R.id.study_fragment, loginFragment);
                    } else {
                        ft.show(loginFragment);
                    }
                }else{
                    ft.remove(loginFragment);
                    if (myInfoFragment == null) {
                        myInfoFragment = new MyInfoFragment();
                        ft.add(R.id.study_fragment, myInfoFragment);
                    } else {
                        ft.show(myInfoFragment);
                    }
                }
            default:
                break;
        }
        ft.commit();
    }
}
